﻿using ChartAI.DTO.Users;
using ChartAI.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Furion.LinqBuilder;
using ChartAI.Core.CM;
using SqlSugar.Extensions;

namespace ChartAI.Application.Users.Services
{
    public class UsersServiceDM : IUsersServiceDM, ITransient
    {
        ISqlSugarClient c;
        public UsersServiceDM(ISqlSugarClient db)
        {
            c = db;
        }
        public ResultPageEntity<UsersDTO> GetUsersList(Request_Users dto)
        {
            var expr = Expressionable.Create<ChartAI_Users>();
            if (!dto.UserName.IsNullOrEmpty())
            {
                expr = expr.And(n => n.UserName == dto.UserName);
            }
            if (!dto.Phone.IsNullOrEmpty())
            {
                expr = expr.And(n => n.Phone == Convert.ToInt32(dto.Phone));
            }
            if (!dto.AppId.IsNullOrEmpty())
            {
                expr = expr.And(n => n.AppId == dto.AppId);
            }
            if (!dto.FId.IsNullOrEmpty())
            {
                expr = expr.And(n => n.FId == Guid.Parse(dto.FId));
            }

            int count = 0;
            var query = c.Queryable<ChartAI_Users>().Where(expr.ToExpression()).OrderByDescending(px => px.AddTime).ToPageList(dto.PageIndex, dto.PageSize, ref count);
            var list = query.Adapt<List<UsersDTO>>();
            return new ResultPageEntity<UsersDTO>() { Data = list, PageIndex = dto.PageIndex, PageSize = dto.PageSize, Count = count };
        }

        public ResultEntity<bool> AddUsers(UsersModel model)
        {
            ChartAI_Users tp = c.Queryable<ChartAI_Users>().Where(w => w.Phone == model.Phone).First();
            if (tp != null)
            {
                return new ResultEntityUtil<bool>().Failure("手机号已经存在");
            }


            ChartAI_Users data = model.Adapt<ChartAI_Users>();


            data.Id = Guid.NewGuid();



            data.UserName = model.UserName;
            data.Phone = model.Phone;
            data.Pwd = MD5Encryption.Encrypt(model.Pwd);
            data.AppId = model.AppId;
            data.FId = model.FId;
            data.BankCode = model.BankCode;
            data.BankName = model.BankName;
            data.BankAddress = model.BankAddress;
            data.NumberW = 0;
            data.NumberP = 0;


            data.AddTime = DateTime.Now;
            var result = c.Insertable(data).ExecuteCommand();
            if (result > 0)
            {
                return new ResultEntityUtil<bool>().Success(true);

            }
            else
            {
                return new ResultEntityUtil<bool>().Success(false);
            }
        }

        public ResultEntity<bool> DeUsers(Guid id)
        {
            return null;
            var result = c.Deleteable<ChartAI_Users>().In(id).ExecuteCommand();
            if (result > 0)
            {
                return new ResultEntityUtil<bool>().Success(true);
            }
            else
            {
                return new ResultEntityUtil<bool>().Success(false);
            }
        }

        public UsersDTO GetUset(Guid id)
        {
            return c.Queryable<ChartAI_Users>().First(x => x.Id == id).Adapt<UsersDTO>();

        }
        public ResultEntity<bool> UpUsers(UsersModel model)
        {
            ChartAI_Users tp = c.Queryable<ChartAI_Users>().Where(w => w.Phone == model.Phone && w.Id != model.Id).First();
            if (tp != null)
            {
                return new ResultEntityUtil<bool>().Failure("手机号已经存在");
            }

            var data = c.Queryable<ChartAI_Users>().InSingle(model.Id);

            data.UserName = model.UserName;
            //data.Phone = model.Phone;
            data.Pwd = MD5Encryption.Encrypt(model.Pwd);
            data.AppId = model.AppId;
            data.FId = model.FId;
            data.BankCode = model.BankCode;
            data.BankName = model.BankName;
            data.BankAddress = model.BankAddress;

            var result = c.Updateable(data).ExecuteCommand();
            if (result > 0)
            {
                return new ResultEntityUtil<bool>().Success(true);
            }
            else
            {
                return new ResultEntityUtil<bool>().Success(false);
            }
        }

        public ResultEntity<MoneyDTO> GetUserM(Guid id)
        {
            MoneyDTO data = new MoneyDTO();
            data.ZM = c.Queryable<ChartAI_CzRecord, ChartAI_Users>((a, b) => a.UserId == b.Id).Where((a, b) => b.FId == id).Sum((a, b) => a.Money).ObjToDecimal();
            if (data.ZM != 0)
            {
                decimal flbl =  App.GetConfig<string>("FlConfigs").ObjToDecimal();
                data.ZM = flbl * data.ZM;
            }
            data.YM = c.Queryable<ChartAI_TxRecord, ChartAI_Users>((a, b) => a.UserId == b.Id).Where((a, b) => b.FId == id).Sum((a, b) => a.Money2).ObjToDecimal();
            data.KM = data.ZM - data.YM;

            return new ResultEntityUtil<MoneyDTO>().Success(data);

        }
    }
}
